package controller.admin;

import model.Goods;
import service.GoodsService;
import utils.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

@WebServlet(name = "AdminGoodsAddServlet", value = "/admin/goods_add")
@MultipartConfig()
public class AdminGoodsAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //图片存的位置
        String directory = "picture" + "/";
        //返回web应用程序文档根目录
        String path = this.getServletContext().getRealPath("/" + directory);

        //获取参数
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String intro = request.getParameter("intro");
        String stock = request.getParameter("stock");
        String typeid = request.getParameter("typeid");

        Part cover = request.getPart("cover");
        Part image1 = request.getPart("image1");
        Part image2 = request.getPart("image2");

        //必填项
        if (!(name != null && !"".equals(name) && image1 != null && image2 != null && cover != null)) {
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }

        //封装数据
        Goods goods = new Goods();
        goods.setName(name);
        goods.setIntro(intro);
        if (price != null && !"".equals(price)) goods.setPrice(Float.parseFloat(price));
        if (stock != null && !"".equals(stock)) goods.setStock(Integer.parseInt(stock));
        if (typeid != null && !"".equals(typeid)) goods.setTypeid(Integer.parseInt(typeid));

        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        //文件名
        String coverName = FileUtils.getShortUUID(cover);
        String image1Name = FileUtils.getShortUUID(image1);
        String image2Name = FileUtils.getShortUUID(image2);

        //存到数据库的数据 目录拼接文件名
        goods.setCover(directory + coverName);
        goods.setImage1(directory + image1Name);
        goods.setImage2(directory + image2Name);

        cover.write(path + coverName);
        image1.write(path + image1Name);
        image2.write(path + image2Name);

        boolean flag = new GoodsService().addGoods(goods);
        //如果添加失败就把上传的文件删除
        if (!flag) {
            File[] file1 = new File[]{
                    new File(path + coverName),
                    new File(path + image1Name),
                    new File(path + image2Name),
            };
            for (File file2 : file1) {
                if (!file2.exists()) {
                    continue;
                }
                if (file2.isFile()) {
                    file2.delete();
                }
            }
        } else { //保存一份上传文件到自己的源码目录
            String desPathStr = "D:" + File.separator + "DOME\\文档\\Practice\\Java Web\\蛋糕商场\\web\\picture";
            FileUtils.copyFile(path + coverName, desPathStr);
            FileUtils.copyFile(path + image1Name, desPathStr);
            FileUtils.copyFile(path + image2Name, desPathStr);
        }
        response.sendRedirect("goods_list");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

}
